package com.muyu.danmo.controller;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoInformation;
import com.muyu.danmo.domain.DanmoPortfolio;
import com.muyu.danmo.req.DanmoPersonalPicturesReq;
import com.muyu.danmo.req.DanmoPortfolioReq;
import com.muyu.danmo.service.IDanmoInformationService;
import com.muyu.danmo.service.IDanmoPersonalPicturesService;
import com.muyu.danmo.service.IDanmoPortfolioService;
import com.muyu.danmo.service.impl.DanmoInformationServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作品集Controller
 *
 * @author yangle
 * @date 2024-09-21
 */

@RestController
@RequestMapping("/portfolio")
public class DanmoPortfolioController extends BaseController
{
    @Autowired
    private IDanmoPortfolioService danmoPortfolioService;

    @Autowired
    private IDanmoInformationService IDanmoInformationService;
    /**
     * 查询作品集列表
     */
//    @RequiresPermissions("system:portfolio:list")
    @PostMapping("/list")
    public Result list(@RequestBody DanmoPortfolio danmoPortfolio)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser==null){
            return Result.notLoggedIn("请登录");
        }
        //查询个人修改后的
        Long informationId = loginUser.getUserid();
        DanmoInformation danmoInformation1 = new DanmoInformation();
        danmoInformation1.setUserId(informationId);
        List<DanmoInformation> danmoInformations = IDanmoInformationService.selectDanmoInformationList(danmoInformation1);
        if (danmoInformations.size()==0){
            return Result.error("请先添加个人信息");
        }

        for (DanmoInformation danmoInformation : danmoInformations) {
            danmoPortfolio.setInformationId(danmoInformation.getId());
        }



        List<DanmoPortfolio> list = danmoPortfolioService.selectDanmoPortfolioList(danmoPortfolio);
        return Result.success(list);
    }

    /**
     * 导出作品集列表
     */
//    @RequiresPermissions("system:portfolio:export")
    @Log(title = "作品集", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoPortfolio danmoPortfolio)
    {
        List<DanmoPortfolio> list = danmoPortfolioService.selectDanmoPortfolioList(danmoPortfolio);
        ExcelUtil<DanmoPortfolio> util = new ExcelUtil<DanmoPortfolio>(DanmoPortfolio.class);
        util.exportExcel(response, list, "作品集数据");
    }

    /**
     * 获取作品集详细信息
     */
//    @RequiresPermissions("system:portfolio:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id)
    {
        return danmoPortfolioService.selectDanmoPortfolioById(id);
    }

    /**
     * 新增作品集
     */
//    @RequiresPermissions("system:portfolio:add")
    @Log(title = "作品集", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody DanmoPortfolio danmoPortfolio)
    {
        return danmoPortfolioService.insertDanmoPortfolio(danmoPortfolio);
    }

    /**
     * 修改作品集
     */
//    @RequiresPermissions("system:portfolio:edit")
    @Log(title = "作品集", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody DanmoPortfolio danmoPortfolio)
    {
        return danmoPortfolioService.updateDanmoPortfolio(danmoPortfolio);
    }

    /**
     * 删除作品集
     */
//    @RequiresPermissions("system:portfolio:remove")
    @Log(title = "作品集", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids)
    {
        return danmoPortfolioService.deleteDanmoPortfolioByIds(ids);
    }



    /**
     * 查询作品id
     */
    @PostMapping("/selectDanmoPersonalPictures")
    public Result selectDanmoPersonalPictures(@RequestBody DanmoPersonalPicturesReq danmoPersonalPicturesReq)
    {
        return success(  danmoPortfolioService.selectDanmoPersonalPictures(danmoPersonalPicturesReq));
    }


    @Autowired
    private DanmoInformationServiceImpl danmoInformationService;

    @Autowired
    private IDanmoPersonalPicturesService danmoPersonalPicturesService;
    /**
     *
     * 查询个人作品集
     */
    @PostMapping("/selectInformationPortfolio")
    public Result selectInformationPortfolio()
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser==null){
            return Result.notLoggedIn("请登录");
        }
        DanmoInformation danmoInformation = danmoInformationService.selectDanmoInformationUserId(loginUser.getUserid());
        if (danmoInformation==null){
            return Result.error("请先完善个人信息");
        }
        DanmoPortfolioReq danmoPortfolioReq = new DanmoPortfolioReq(danmoInformation.getId());
        return Result.success( danmoPersonalPicturesService.selectDanmoPersonalPictures(danmoPortfolioReq));
    }

    /**
     *
     * 查询他人作品集
     */
    @PostMapping("/selectInformationPortfolioByOthers")
    public Result selectInformationPortfolioByOthers(@RequestBody DanmoPortfolioReq danmoPortfolioReq)
    {
        return Result.success(danmoPersonalPicturesService.selectDanmoPersonalPictures(danmoPortfolioReq));
    }

    /**
     * 作品下架
     */
//    @RequiresPermissions("system:collectShellfish:edit")
//    @Log(title = "拾贝下架", businessType = BusinessType.UPDATE)
    @GetMapping("getinfo/{id}")
    public Result removed(@PathVariable("id") Long id)
    {
        return danmoPortfolioService.updateDanmoCollectShellfishById(id);
    }

    /**
     * 作品重新上架
     */
//    @RequiresPermissions("system:collectShellfish:edit")
    @Log(title = "拾贝下架", businessType = BusinessType.UPDATE)
    @GetMapping("listed/{id}")
    public Result listed (@PathVariable("id") Long id)
    {
        return danmoPortfolioService.updateDanmoCollectShellfishListedById(id);
    }
}